﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Mengsk.ProxyDelivery.App.Extenstions;
using Mengsk.ProxyDelivery.Domain;

namespace Mengsk.ProxyDelivery.App.UI.Seller
{
    /// <summary>
    /// SellerShop.xaml 的交互逻辑
    /// </summary>
    public partial class SellerShopUserControl : UserControl, IPlugin
    {
        private SellerShopService sellerShopService = ServiceContainer.GetService<SellerShopService>();

        public SellerShopUserControl()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return "卖家店铺"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.cbbPopTypes.Bind<PopType>();
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            this.pbBar.Parameters.Clear();
            this.pbBar.Parameters.Add("popType", this.cbbPopTypes.SelectedIndex);

            int sellerId=0;
            int.TryParse(this.tbSellerId.Text,out sellerId);
            this.pbBar.Parameters.Add("sellerId", sellerId);
            this.pbBar.Parameters.Add("popSellerName", this.tbPopSellerName.Text.Trim());
            this.pbBar.Parameters.Add("popShopName", this.tbPopShopName.Text.Trim());
            this.pbBar.StartPage();
        }

        private void pbBar_PageChanging(object sender, PageBar.PageChangeEventArgs e)
        {
            var data = this.sellerShopService.GetByAll(e.GetParameter<PopType>("popType"), e.GetParameter<int>("sellerId"), e.GetParameter<string>("popSellerName"), e.GetParameter<string>("popShopName"), e.CurrentPage - 1, e.PageSize);
            this.pbBar.Total = data.Total;
            this.pbBar.CurrentCount = data.Datas.Count;
            this.dgvShop.ItemsSource = data.Datas;
        }

        private void Open_Click(object sender, EventArgs e)
        {
            try
            {
                TextBlock tb = sender as TextBlock;
                if (tb == null)
                {
                    throw new Exception("Edit_Click错误，事件源不是TextBlock");
                }
                Mengsk.ProxyDelivery.Domain.SellerShop vendor = tb.DataContext as Mengsk.ProxyDelivery.Domain.SellerShop;
                Process.Start(vendor.PopShopUrl);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


    }
}
